import { getCart, isCheckTrue, getCartItem, setCartItem, setCartItemCheck, setCartItemCheckAll, setCartItemCount, delCartItem, batchDelCartItem } from '@/api/cart/cart'
import { getId } from '@/utils/userKey-token'
import {getUserKey} from "@/utils/user-key";
import {getToken} from "@/utils/login-token";

const state = {
  // userKey: getUserKey()
}

const mutations = {

}

const actions = {
  getCart({ commit }) {
    return new Promise((resolve, reject) => {
      getCart(getId())
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  isCheckTrue({ commit }) {
    return new Promise((resolve, reject) => {
      isCheckTrue(getId())
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  getCartItem({ commit }) {
    return new Promise((resolve, reject) => {
      getCartItem(getUserKey(), getToken())
        .then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
    })
  },
  setCartItem({ commit }, query) {
    console.log(query)
    return new Promise((resolve, reject) => {
      setCartItem(query.skuId, getId(), query.count)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  setCartItemCheck({ commit }, skuId) {
    return new Promise((resolve, reject) => {
      setCartItemCheck(getId(), skuId)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  setCartItemCheckAll({ commit }, check) {
    return new Promise((resolve, reject) => {
      setCartItemCheckAll(getId(), check)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  setCartItemCount({ commit }, list) {
    return new Promise((resolve, reject) => {
      setCartItemCount(getId(), list.skuId, list.count)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  delCartItem({ commit }, skuId) {
    return new Promise((resolve, reject) => {
      delCartItem(getId(), skuId)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  },
  batchDelCartItem({ commit }, skuId) {
    return new Promise((resolve, reject) => {
      batchDelCartItem(getId(), skuId)
        .then(response => {
          resolve(response)
        }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
